
#include "stopwatch.hpp"
#include <ctime>
#include <cassert>

void stopwatch::start()
{
	if(rng || (data == 0))
		return;
	((std::clock_t*)data)[0] = std::clock();
	rng = true;
}

void stopwatch::stop()
{
	if(!rng || (data == 0))
		return;
	((std::clock_t*)data)[1] = std::clock();
	rng = false;
	calc_diff();
}

void stopwatch::calc_diff()
{
	assert(!rng && (data != 0));
	std::clock_t & beg = ((std::clock_t*)data)[0];
	std::clock_t & end = ((std::clock_t*)data)[1];
	curr = end - beg;
}

const unsigned long long int stopwatch::read()
{
	if(!rng) return curr;
	std::clock_t now = std::clock();
	std::clock_t & beg = ((std::clock_t*)data)[0];
	return now - beg;
}

void stopwatch::initialize_data()
{
	assert(data == 0);
	data = new std::clock_t[2];
}

stopwatch::~stopwatch()
{
	if(data)
	{
		delete [] (std::clock_t*)(data);
		data = 0;
	}
}

